package initialzation

import (
	"server/middleware"
	"server/myutils"
	"server/routes"
	"time"

	_ "server/docs"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

func InitGin() {
	gin.SetMode(myutils.AppMode)
	gin.ForceConsoleColor()
	var app = gin.New()
	app.Use(cors.New(
		cors.Config{
			AllowAllOrigins:  true,
			AllowMethods:     []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"},
			AllowHeaders:     []string{"Origin", "Content-Length", "Content-Type", "Authorization"}, //增加一个允许跨域的请求头
			AllowCredentials: false,
			MaxAge:           12 * time.Hour,
		},
	))

	app.Use(middleware.Logger(), gin.Recovery())
	routes.Routes(app)
	app.GET("/apidoc/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	app.Run(myutils.HttpPost)
}
